package com.situ.fly.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.situ.fly.entity.Question;

@Mapper
public interface QuestionMapper {

	/**
	 * 插入一条问题
	 * @param question
	 * @return
	 */
	@Insert("insert into question (title, content, type, kiss, uid) "
			+ "value(#{title}, #{content}, #{type}, #{kiss}, #{uid})")
	@Options(useGeneratedKeys = true, //  使用自动生成的主键
		keyProperty = "id"		// 取出自动生成的主键, 存放到question 参数的id属性中
	)
	int insert(Question question);
	
	/**
	 * 修改帖子
	 * @param question
	 * @return
	 */
	@Update("<script>update question "
			+ "<set> "
			+ "<if test='type!=null'>type=#{type}, </if>"
			+ "<if test='title!=null'>title=#{title}, </if>"
			+ "<if test='content!=null'>content=#{content}, </if>"
			+ "<if test='status!=null'>status=#{status}, </if>"
			+ "</set>"
			+ "where id=#{id}"
			+ "</script>")
	int update(Question question);

	/**
	 * 根据ID, 查询问题
	 * @param id
	 * @return
	 */
	@Select("select * from question where id=#{id}")
	Question selectById(Integer id);

	/**
	 * 根据用户, 查询问题
	 * @param uid
	 * @return
	 */
	@Select("select * from question where uid=#{uid} order by createtime desc")
	List<Map<String, Object>> selectByUid(Integer uid);

	/**
	 * 根据ID, 修改指定字段的值
	 * @param field
	 * @param rank
	 * @param id
	 * @return
	 */
	@Update("update question set ${field}=#{rank} where id=#{id}")
	int updateByField(String field, Integer rank, Integer id);

	/**
	 * 更新浏览数量
	 * @param question
	 */
	@Update("update question set viewsnum=#{viewsnum} where id=#{id}")
	void updateViewsnum(Question question);

	/**
	 * 查询置顶的问题
	 * @param question 
	 * @param order 
	 * @return
	 */
	@Select("<script> select * from question "
			+ "<where>"
			+ "<if test='question.level!=null'>level=#{question.level} </if>"
			+ "<if test='question.status!=null'> and status=#{question.status}</if>"
			+ "<if test='question.type!=null'> and type=#{question.type}</if>"
			+ "</where>"
			+ "<if test='order!=null'> order by ${order} desc</if>"
			+ "</script>")
	/*
	 * 上面这是一个动态SQL
	 * level 不是空:  select * from question where level=#{level}
	 * level 是空: 	select * from question 
	 */
	
	// 关联查询的映射
	@Results({
		@Result(column = "id", property = "id", id = true),
		@Result(column = "uid", property = "uid"),
		@Result(column = "uid", property = "user", 
		one = @One(select = "com.situ.fly.mapper.UserMapper.selectById"))})
	List<Question> select(Question question, String order);

	/**
	 * 根据指定时间, 查询最近的回复数量的问题排行
	 * @param startTime
	 * @param limit
	 * @return
	 */
	@Select("select question.*, count(answer.id) as replynum from question " + 
			"left join answer on question.id=answer.qid " + 
			"where answer.createtime >= #{startTime} " + 
			"group by question.id " + 
			"order by count(answer.id) desc " + 
			"limit #{limit}")
	List<Map<String, Object>> selectByReplyTime(Date startTime, Integer limit);


}





